package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R login(HttpSession session, @RequestBody Employee employee){
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(qw);
        if (one==null){
            return R.error("用户名不存在");
        }
        String rawPad = employee.getPassword();
        String dbPaw = one.getPassword();
        String pwd = DigestUtils.md5DigestAsHex(rawPad.getBytes());
        if (!dbPaw.equals(pwd)){
            return R.error("密码错误");
        }
        Integer status = one.getStatus();
        if (status==0){
            return R.error("账号已禁用");
        }
        session.setAttribute("loginEmployee",one.getId());
        return R.success(one);
    }


    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }


    @PostMapping
    public R addEmployee(HttpSession session, @RequestBody Employee employee){
        String md5DigestAsHex = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(md5DigestAsHex);

        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        Long loginEmployee = (Long) session.getAttribute("loginEmployee");
        employee.setCreateUser(loginEmployee);
        employee.setUpdateUser(loginEmployee);

        employeeService.save(employee);
        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R pageEmployee(int page,int pageSize,String name){
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Employee::getUsername, name);
        employeeLambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,employeeLambdaQueryWrapper);
        return R.success(pageInfo);
    }


    @PutMapping
    public R updateEmployee(HttpSession Session, @RequestBody Employee employee){
        employee.setUpdateTime(LocalDateTime.now());
        Long loginEmployee = (Long) Session.getAttribute("loginEmployee");
        employee.setUpdateUser(loginEmployee);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }


    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return R.success(employee);
        }
        return R.error("查询失败");
    }
}
